package com.base.controller.dsky;

import com.base.log.WebLog;
import com.base.po.param.NetState;
import com.base.po.voip.Subscribers;
import com.base.service.MoneyGoldService;
import com.base.service.SubscribersService;
import com.beust.jcommander.internal.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static java.util.stream.Collectors.toList;

@Slf4j
@Api(tags = "金积分")
@RestController
@RequestMapping("/moneyGold")
public class MoneyGoldController {
    private final MoneyGoldService moneyGoldService;
    private final SubscribersService subscribersService;

    public MoneyGoldController(MoneyGoldService moneyGoldService, SubscribersService subscribersService) {
        this.moneyGoldService = moneyGoldService;
        this.subscribersService = subscribersService;
    }

    @WebLog(description = "获取个人持有总金积分")
    @ApiOperation(value = "获取个人持有总金积分")
    @PostMapping("/getGoldMoneyByUserId")
    public NetState getGoldMoneyByUserId(@ApiParam("用户ID") @RequestParam String userId){
     return new NetState(NetState.SUCCESS_CONN,"查询成功",moneyGoldService.getGoldMoneyByUserId(userId,null).setScale(2, RoundingMode.HALF_EVEN));
    }

    @WebLog(description = "获取个人持有总金积分详情")
    @ApiOperation(value = "获取个人持有总金积分详情")
    @PostMapping("/getGoldMoneyDetailByUserId")
    public NetState getGoldMoneyDetailByUserId(@ApiParam("用户ID") @RequestParam String userId, @ApiParam("当前页") @RequestParam Integer pageNo, @ApiParam("页码") @RequestParam Integer pageSize){
        return new NetState(NetState.SUCCESS_CONN,"查询成功",moneyGoldService.getGoldMoneyDetailByUserId(userId,null,pageNo,pageSize));
    }

    @WebLog(description = "转账金积分")
    @ApiOperation(value = "转账金积分")
    @PostMapping("/transferGoldMoneyDetailByUserId")
    public NetState getMoneyDetailByUserId(@ApiParam("转账用户Id") @RequestParam String userId, @ApiParam("用户ID") @RequestParam BigDecimal money, @ApiParam("接受账户手机号") @RequestParam String mobile, @ApiParam(value = "加密后的密码") @RequestParam String password){
        //查询当前人的金额:
        BigDecimal goldMoneyAll=moneyGoldService.getGoldMoneyByUserId(userId,null);
        if(goldMoneyAll.compareTo(money)<0){
            return new NetState(NetState.FEAILED,"当前转账团长分红不足",null);
        }
        Subscribers sub= subscribersService.selectBySubscribers(mobile,null);
        if (sub == null){
            return new NetState(NetState.FEAILED,"接受转账用户不存在",null);
        }
        List<String> subLists1=Optional.ofNullable(subscribersService.findParList(userId)).orElse(Lists.newArrayList());
        List<String> subLists2=Optional.ofNullable(subscribersService.findChildList(userId)).orElse(Lists.newArrayList());
        subLists1.addAll(subLists2);
        List<String> getList=subLists1.stream().filter(item->item.equals(sub.getId())).collect(toList());
        if (getList.size() <1){
            return new NetState(NetState.FEAILED,"不在同一网体内");
        }
        //开始转账
        return moneyGoldService.transfer(userId,sub.getId(),money);
    }
}
